﻿<%@ WebHandler Language="C#" Class="SyzyyHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Xml;
using Dln.Common.Helper;
using Dln.Common;
using Dln.Model;
using Dln.Handler;
using Dln.Model.WebService.Model;
using Dos.ORM;
using Newtonsoft.Json;
using ServiceStack;

public class SyzyyHandler : HandlerBase
{
    public string url = CommonHelper.GetAppSettings("DlnService1");

    /// <summary>
    /// 新增就诊人
    /// </summary>
    public void AddPatientInfo()
    {
        string openid = _httpReuqestString["openid"];
        string type = _httpReuqestString["type"];
        string name = _httpRequestForm["realName"];
        string idCard = _httpRequestForm["idCard"];
        string cardNo = _httpRequestForm["patientCard"];
        string phone = _httpRequestForm["phone"];

        object[] arge = new object[4];
        arge[0] = name;
        arge[1] = idCard;
        arge[2] = phone;
        arge[3] = cardNo;

        object getCurrInHospNum = WebServiceHelper.InvokeWebService(url, "AddPerson", arge);
        if (getCurrInHospNum != null)
        {
            string xml = "<outContent>" + getCurrInHospNum.ToString() + "</outContent>";
            XmlDocument x = new XmlDocument();
            x.LoadXml(xml);
            XmlNodeList nodeList = x.SelectNodes("outContent");
            string a = "";
            var result = nodeList[0].ChildNodes[0].InnerText;
            var message = nodeList[0].ChildNodes[1].InnerText;
            if (Convert.ToInt32(result) == 0)
            {
                PrintErrorJson(message);
            }
            else
            {
                var uptModel =
                    SqlHelper.Context.From<PatientInfo>()
                        .Where(d => d.PatientPatiID == cardNo && d.Delstatus == 0 && d.Openid == openid)
                        .First();
                var uptModel1 =
                    SqlHelper.Context.From<PatientInfo>()
                        .Where(d => d.PatientPatiID == cardNo && d.Delstatus == 1 && d.Openid == openid)
                        .First();
                if (type == "add")
                {
                    if (uptModel != null)
                    {
                        PrintErrorJson("添加失败，已存在该就诊人！");
                    }
                    if (uptModel1 != null)
                    {
                        uptModel1.Delstatus = 0;
                        uptModel1.PatientIdCard = idCard;
                        uptModel1.PatientPhone = phone;
                        uptModel1.PatientName = name;
                        uptModel1.PatientPatiID = cardNo;
                        uptModel1.Openid = openid;
                        var updateResult = SqlHelper.Context.Update<PatientInfo>(uptModel1);
                        if (updateResult > 0)
                        {
                            PrintSuccessJson("添加成功！");
                        }
                        else
                        {
                            PrintErrorJson("添加失败，请联系中医院信息科！");
                        }
                    }

                    var model = new PatientInfo();
                    model.AddTime = DateTime.Now;
                    model.PatientIdCard = idCard;
                    model.PatientPhone = phone;
                    model.PatientName = name;
                    model.PatientPatiID = cardNo;
                    model.Openid = openid;
                    var insertResult = SqlHelper.Context.Insert<PatientInfo>(model);
                    if (insertResult > 0)
                    {
                        PrintSuccessJson("添加成功！");
                    }
                    else
                    {
                        PrintErrorJson("添加失败，请联系中医院信息科！");
                    }
                }
                else
                {
                    uptModel.PatientIdCard = idCard;
                    uptModel.PatientPhone = phone;
                    uptModel.PatientName = name;
                    uptModel.PatientPatiID = cardNo;

                    var updateResult = SqlHelper.Context.Update<PatientInfo>(uptModel);
                    if (updateResult > 0)
                    {
                        PrintSuccessJson("修改成功！");
                    }
                    else
                    {
                        PrintErrorJson("修改失败，请联系中医院信息科！");
                    }
                }


            }
        }
        PrintErrorJson("添加失败，请联系中医院信息科！");
    }

    /// <summary>
    /// 删除就诊人
    /// </summary>
    public void DeletePatientInfo()
    {
        string openid = _httpReuqestString["openid"];
        string cardNo = _httpReuqestString["cardNo"];
        var delModel =
            SqlHelper.Context.From<PatientInfo>()
                .Where(d => d.PatientPatiID == cardNo && d.Delstatus == 0 && d.Openid == openid)
                .First();
        if (delModel != null)
        {
            delModel.Delstatus = 1;
            var updateResult = SqlHelper.Context.Update<PatientInfo>(delModel);
            if (updateResult > 0)
            {
                PrintSuccessJson("删除成功！");
            }
            else
            {
                PrintErrorJson("删除失败，请联系中医院信息科！");
            }

        }
        PrintErrorJson("删除失败，未找到该就诊人信息！");
    }

    /// <summary>
    /// 查询住院费用
    /// </summary>
    public void SearchInHospitalMoney()
    {
        string name = _httpRequestForm["realName"];
        string cardNo = _httpRequestForm["patientCard"];

        object[] arge = new object[2];
        arge[0] = name;
        arge[1] = cardNo;

        object getCurrInHospNum = WebServiceHelper.InvokeWebService(url, "GetSubscription", arge);
        if (getCurrInHospNum != null)
        {
            var isExistReturn = getCurrInHospNum.ToString().Contains("return");
            if (isExistReturn)
            {
                string xml = "<outContent>" + getCurrInHospNum.ToString() + "</outContent>";
                XmlDocument x = new XmlDocument();
                x.LoadXml(xml);
                XmlNodeList nodeList = x.SelectNodes("outContent");
                string a = "";
                var message = nodeList[0].ChildNodes[1].InnerText;
                PrintErrorJson(message);
            }
            else
            {
                XmlDocument x = new XmlDocument();
                x.LoadXml(getCurrInHospNum.ToString());
                XmlNodeList nodeList = x.SelectNodes("table");
                string a = "";
                if (nodeList != null)
                {
                    var returnFirst = nodeList[0].ChildNodes[2].InnerText;
                    var returnSecond = nodeList[0].ChildNodes[3].InnerText;
                    PrintSuccessJson(string.Format("InHospitalMoneyList.aspx?returnFirst={0}&returnSecond={1}",
                        returnFirst, returnSecond));
                }
                PrintErrorJson("未查到该住院费用信息！");
            }

        }
    }

    /// <summary>
    /// 查询费用详细
    /// </summary>
    public void SearchInHospitalMoneyDetial()
    {
        string openid = _httpReuqestString["openid"];
        string cardNo = _httpReuqestString["cardNo"];
        string date = _httpReuqestString["date"];

        var model =
                SqlHelper.Context.From<PatientInfo>()
                    .Where(d => d.PatientPatiID == cardNo && d.Delstatus == 0 && d.Openid == openid)
                    .First();
        if (model != null)
        {
            object[] arge = new object[3];
            arge[0] = model.PatientName;
            arge[1] = cardNo;
            arge[2] = date;

            object getCurrInHospNum = WebServiceHelper.InvokeWebService(url, "GetADay", arge);
            if (getCurrInHospNum != null)
            {
                var isExistReturn = getCurrInHospNum.ToString().Contains("return");
                if (isExistReturn)
                {
                    string xml = "<outContent>" + getCurrInHospNum.ToString() + "</outContent>";
                    XmlDocument x = new XmlDocument();
                    x.LoadXml(xml);
                    XmlNodeList nodeList = x.SelectNodes("outContent");
                    string a = "";
                    var message = nodeList[0].ChildNodes[1].InnerText;
                    PrintErrorJson(message);
                }
                else
                {
                    XmlDocument x = new XmlDocument();
                    x.LoadXml(getCurrInHospNum.ToString());
                    XmlNodeList nodeList = x.SelectNodes("NewDataSet");
                    string a = "";
                    if (nodeList != null)
                    {
                        IList<GetADayModel> ModelList = new List<GetADayModel>();

                        for (var i = 0; i < nodeList[0].ChildNodes.Count; i++)
                        {
                            GetADayModel getADayModel = new GetADayModel();
                            getADayModel.ITEMID = nodeList[0].ChildNodes[i].ChildNodes[0].InnerText;
                            getADayModel.ITEMNAME = nodeList[0].ChildNodes[i].ChildNodes[1].InnerText;
                            getADayModel.UNITNAME = nodeList[0].ChildNodes[i].ChildNodes[2].InnerText;
                            getADayModel.UNITQUANTITY = Convert.ToDouble(nodeList[0].ChildNodes[i].ChildNodes[3].InnerText).ToString("0");
                            getADayModel.PRICE = Convert.ToDouble(nodeList[0].ChildNodes[i].ChildNodes[4].InnerText).ToString("F2");
                            getADayModel.TOTAMOUNT = Convert.ToDouble(nodeList[0].ChildNodes[i].ChildNodes[5].InnerText).ToString("F2");
                            getADayModel.BILLDATE = nodeList[0].ChildNodes[i].ChildNodes[6].InnerText;
                            ModelList.Add(getADayModel);
                        }
                        HttpContext.Current.Response.Write(JsonConvert.SerializeObject(ModelList));
                        HttpContext.Current.Response.End();
                    }

                }

            }
        }
    }
}